package com.ruowei.modules.sys.repository;

import com.ruowei.common.repository.BaseRepository;
import com.ruowei.modules.sys.domain.entity.SysUser;
import com.ruowei.modules.sys.domain.enumeration.UserStatusType;
import com.ruowei.modules.sys.domain.enumeration.UserType;
import com.ruowei.modules.sys.domain.table.QSysUserTable;
import com.ruowei.modules.sys.domain.table.SysUserTable;
import jdk.net.SocketFlow;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.Size;
import java.util.List;
import java.util.Optional;


/**
 * Spring Data  repository for the SysUser entity.
 *
 * @author 刘东奇
 */
@Repository
public interface SysUserRepository
    extends BaseRepository<Long, SysUserTable, QSysUserTable> {

    /**
     * 通过usercode倒叙找第一个Sysuser
     *
     * @author 刘东奇
     * @date 2019/9/25
     */
    Optional<QSysUserTable> findFirstByUserCodeNotNullOrderByUserCodeDesc();

    SysUserTable findFirstByUserCode(@Size(max = 100) String userCode);

    Optional<SysUserTable> findByUserName(String userName);

    Optional<SysUserTable> findByLoginCode(String loginCode);

    Optional<QSysUserTable> findOneByLoginCode(String login);

    List<SysUserTable> findAllByIdInAndStatusNot(List<Long> ids, UserStatusType userStatusType);

    List<SysUserTable> findAllByStatusAndUserCodeIsNotNull(UserStatusType userStatusType);

    @Query("select distinct u.phone from SysUser u" +
        " left join SysUserRole ur on u.id = ur.sysUserId" +
        " left join SysRole r on ur.sysRoleId = r.id" +
        " where r.roleCode like ?1 and u.status = 'NORMAL' and u.phone is not null")
    List<String> findAllUserPhoneByRoleCodeLike(String roleCode);

    @Query("select distinct u from SysUser u" +
        " left join SysUserRole ur on u.id = ur.sysUserId" +
        " left join SysRole r on ur.sysRoleId = r.id" +
        " left join SysEmployee e on u.employee.id = e.id" +
        " where e.office.officeCode = ?1 and r.roleCode like ?2 and u.status = 'NORMAL'")
    List<SysUser> findAllByOfficeCodeAndRoleCodeLike(String officeCode, String roleCode);

    @Query("select distinct u from SysUser u" +
        " left join SysUserRole ur on u.id = ur.sysUserId" +
        " left join SysRole r on ur.sysRoleId = r.id" +
        " left join SysEmployee e on u.employee.id = e.id" +
        " where e.office.officeCode = ?1 and r.roleCode in ?2 and u.status = 'NORMAL'")
    List<SysUser> findAllByOfficeCodeAndRoleCodeIn(String officeCode, List<String> roleCode);

    @Query("select distinct u from SysUser u" +
        " left join SysEmployee e on u.employee.id = e.id" +
        " where e.office.officeCode = ?1 and u.id in ?2 and u.status = 'NORMAL'")
    List<SysUser> findAllByOfficeAndIds(String officeCode, List<Long> userIds);

    List<SysUserTable> findAllByRefCodeIn(List<Long> refCodes);

    void deleteAllByIdIn(List<Long> ids);

    @Query("select count(u.id) from SysUser u" +
        " left join SysUserRole ur on u.id = ur.sysUserId" +
        " left join SysRole r on ur.sysRoleId = r.id" +
        " where u.deleted = false and u.status = 'NORMAL' and r.roleCode = 'BJG'")
    Long countAllByDeletedIsFalseAndStatusAndRoleCode(String roleCode);

    Boolean existsByUserCodeAndUserTypeAndStatus(String userCode, UserType userType, UserStatusType status);

    Boolean existsByUserCode(String userCode);

    Boolean existsByLoginCodeAndStatus(String userCode, UserStatusType status);

    Boolean existsByLoginCodeAndStatusAndIdNot(String userCode, UserStatusType status, Long id);

    Optional<SysUserTable> findOneByUserCode(String userCode);
}
